home *** CD-ROM | disk | FTP | other *** search
/ Aminet 52 / Aminet 52 (2002)(GTI - Schatztruhe)[!][Dec 2002].iso / Aminet / util / moni / Scout-src.lha / source / objects / scout_catalogs.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-09-17  |  11.5 KB  |  344 lines

  1. /**
  2.  * Scout - The Amiga System Monitor
  3.  *
  4.  *------------------------------------------------------------------
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, write to the Free Software
  18.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  * You must not use this source code to gain profit of any kind!
  21.  *
  22.  *------------------------------------------------------------------
  23.  *
  24.  * @author Andreas Gelhausen
  25.  * @author Richard Körber <rkoerber@gmx.de>
  26.  */
  27.  
  28. #include "system_headers.h"
  29.  
  30. struct CatalogsCallbackUserData {
  31.     APTR ud_List;
  32.     ULONG ud_Count;
  33. };
  34.  
  35. // this structure works with locale.library 44.6
  36. // there is no guarantee that it will work with previous or future versions
  37. struct PrivateLocaleBase {
  38.     struct Library plb_LibNode;
  39.     UBYTE plb_pad[94];
  40.     struct List plb_CatalogList;
  41. };
  42.  
  43. static __asm __saveds LONG catlist_con2func(register __a2 Object *obj, register __a1 struct NList_ConstructMessage *msg, register __a0 struct Hook *hook)
  44. {
  45.     return AllocListEntry(msg->pool, msg->entry, sizeof(struct CatalogEntry));
  46. }
  47.  
  48. MakeHook(catlist_con2hook, catlist_con2func);
  49.  
  50. static __asm __saveds LONG catlist_des2func(register __a2 Object *obj, register __a1 struct NList_DestructMessage *msg, register __a0 struct Hook *hook)
  51. {
  52.     FreeListEntry(msg->pool, &msg->entry);
  53.  
  54.     return 0;
  55. }
  56.  
  57. MakeHook(catlist_des2hook, catlist_des2func);
  58.  
  59. static __asm __saveds LONG catlist_dsp2func(register __a2 Object *obj, register __a1 struct NList_DisplayMessage *msg, register __a0 struct Hook *hook)
  60. {
  61.     struct CatalogEntry *ce = (struct CatalogEntry *)msg->entry;
  62.  
  63.     if (ce) {
  64.         msg->strings[0] = ce->ce_Address;
  65.         msg->strings[1] = ce->ce_Name;
  66.         msg->strings[2] = ce->ce_Version;
  67.         msg->strings[3] = ce->ce_Language;
  68.     } else {
  69.         msg->strings[0] = "Address";
  70.         msg->strings[1] = "cat_Name";
  71.         msg->strings[2] = "cat_Version";
  72.         msg->strings[3] = "cat_Language";
  73.         msg->preparses[0] = MUIX_B;
  74.         msg->preparses[1] = MUIX_B;
  75.         msg->preparses[2] = MUIX_B;
  76.         msg->preparses[3] = MUIX_B;
  77.     }
  78.  
  79.     return 0;
  80. }
  81.  
  82. MakeHook(catlist_dsp2hook, catlist_dsp2func);
  83.  
  84. static LONG catlist_cmp2colfunc( struct CatalogEntry *ce1,
  85.                                 struct CatalogEntry *ce2,
  86.                                 ULONG column )
  87. {
  88.     switch (column) {
  89.         case 0: return stricmp(ce1->ce_Address, ce2->ce_Address);
  90.         case 1: return stricmp(ce1->ce_Name, ce2->ce_Name);
  91.         case 2: return stricmp(ce1->ce_Version, ce2->ce_Version);
  92.         case 3: return stricmp(ce1->ce_Language, ce2->ce_Language);
  93.     }
  94. }
  95.  
  96. static __asm __saveds LONG catlist_cmp2func(register __a2 Object *obj, register __a1 struct NList_CompareMessage *msg, register __a0 struct Hook *hook)
  97. {
  98.     LONG cmp;
  99.     struct CatalogEntry *ce1, *ce2;
  100.     ULONG col1, col2;
  101.  
  102.     ce1 = (struct CatalogEntry *)msg->entry1;
  103.     ce2 = (struct CatalogEntry *)msg->entry2;
  104.     col1 = msg->sort_type & MUIV_NList_TitleMark_ColMask;
  105.     col2 = msg->sort_type2 & MUIV_NList_TitleMark2_ColMask;
  106.  
  107.     if (msg->sort_type == MUIV_NList_SortType_None) return 0;
  108.  
  109.     if (msg->sort_type & MUIV_NList_TitleMark_TypeMask) {
  110.         cmp = catlist_cmp2colfunc(ce2, ce1, col1);
  111.     } else {
  112.         cmp = catlist_cmp2colfunc(ce1, ce2, col1);
  113.     }
  114.  
  115.     if (cmp != 0 || col1 == col2) return cmp;
  116.  
  117.     if (msg->sort_type2 & MUIV_NList_TitleMark2_TypeMask) {
  118.         cmp = catlist_cmp2colfunc(ce2, ce1, col2);
  119.     } else {
  120.         cmp = catlist_cmp2colfunc(ce1, ce2, col2);
  121.     }
  122.  
  123.     return cmp;
  124. }
  125.  
  126. MakeHook(catlist_cmp2hook, catlist_cmp2func);
  127.  
  128. static void ReceiveList( void (* callback)( struct CatalogEntry *ce, void *userData ),
  129.                          void *userData )
  130. {
  131.     struct CatalogEntry *ce;
  132.  
  133.     if (ce = tbAllocVecPooled(globalPool, sizeof(struct CatalogEntry))) {
  134.         if (SendDaemon("GetCatalogList")) {
  135.             while (ReceiveDecodedEntry((UBYTE *)ce, sizeof(struct CatalogEntry))) {
  136.                 callback(ce, userData);
  137.             }
  138.         }
  139.  
  140.         tbFreeVecPooled(globalPool, ce);
  141.     }
  142. }
  143.  
  144. static void IterateList( void (* callback)( struct CatalogEntry *ce, void *userData ),
  145.                          void *userData )
  146. {
  147.     struct PrivateLocaleBase *LocaleBase;
  148.  
  149.     if (LocaleBase = (struct PrivateLocaleBase *)OpenLibrary("locale.library", 38)) {
  150.         struct MinList tmplist;
  151.         struct CatalogEntry *ce, *_ce;
  152.         struct Catalog *cat;
  153.  
  154.         NewList((struct List *)&tmplist);
  155.  
  156.         Forbid();
  157.  
  158.         ITERATE_LIST(&LocaleBase->plb_CatalogList, struct Catalog *, cat) {
  159.             if (ce = AllocVec(sizeof(struct CatalogEntry), MEMF_PUBLIC)) {
  160.                 ce->ce_Addr = cat;
  161.                 _snprintf(ce->ce_Address, sizeof(ce->ce_Address), "$%08lx", cat);
  162.                 stccpy(ce->ce_Name, nonetest(cat->cat_Link.ln_Name), sizeof(ce->ce_Name));
  163.                 _snprintf(ce->ce_Version, sizeof(ce->ce_Version), "%ld.%ld", cat->cat_Version, cat->cat_Revision);
  164.                 stccpy(ce->ce_Language, nonetest(cat->cat_Language), sizeof(ce->ce_Language));
  165.  
  166.                 AddTail((struct List *)&tmplist, (struct Node *)ce);
  167.             }
  168.         }
  169.  
  170.         ITERATE_CHANGING_LIST(&tmplist, struct CatalogEntry *, ce, _ce) {
  171.             callback(ce, userData);
  172.             FreeVec(ce);
  173.         }
  174.  
  175.         CloseLibrary((struct Library *)LocaleBase);
  176.     }
  177. }
  178.  
  179. static void UpdateCallback( struct CatalogEntry *ce,
  180.                             void *userData )
  181. {
  182.     struct CatalogsCallbackUserData *ud = (struct CatalogsCallbackUserData *)userData;
  183.  
  184.     InsertSortedEntry(ud->ud_List, ce);
  185.     ud->ud_Count++;
  186. }
  187.  
  188. static void PrintCallback( struct CatalogEntry *ce,
  189.                            void *userData )
  190. {
  191.     PrintFOneLine((BPTR)userData, " %s   %-5.5s %-10.10s %s\n", ce->ce_Address, ce->ce_Version, ce->ce_Language, ce->ce_Name);
  192. }
  193.  
  194. static void SendCallback( struct CatalogEntry *ce,
  195.                           void *userData )
  196. {
  197.     SendEncodedEntry((UBYTE *)ce, sizeof(struct CatalogEntry));
  198. }
  199.  
  200. static ULONG __saveds mNew( struct IClass *cl,
  201.                             Object *obj,
  202.                             struct opSet *msg )
  203. {
  204.     APTR catlist, cattext, catcount, updateButton, printButton, exitButton;
  205.  
  206.     if (obj = (Object *)DoSuperNew(cl, obj,
  207.         MUIA_HelpNode, CatalogsText,
  208.         MUIA_Window_ID, MakeID('C','A','T','A'),
  209.         WindowContents, VGroup,
  210.  
  211.             Child, catlist = MyNListviewObject(MakeID('C','A','L','V'), "BAR,BAR,BAR P=" MUIX_C ",BAR", &catlist_con2hook, &catlist_des2hook, &catlist_dsp2hook, &catlist_cmp2hook, TRUE),
  212.             Child, MyBelowListview(&cattext, &catcount),
  213.  
  214.             Child, MyVSpace(4),
  215.  
  216.             Child, HGroup, MUIA_Group_SameSize, TRUE,
  217.                 Child, updateButton   = MakeButton(txtUpdate),
  218.                 Child, printButton    = MakeButton(txtPrint),
  219.                 Child, exitButton     = MakeButton(txtExit),
  220.             End,
  221.         End,
  222.         TAG_MORE, msg->ops_AttrList))
  223.     {
  224.         struct CatalogsWinData *cwd = INST_DATA(cl, obj);
  225.         APTR parent;
  226.  
  227.         cwd->cwd_CatalogList = catlist;
  228.         cwd->cwd_CatalogText = cattext;
  229.         cwd->cwd_CatalogCount = catcount;
  230.  
  231.         parent = (APTR)GetTagData(MUIA_Window_ParentWindow, (ULONG)NULL, msg->ops_AttrList);
  232.  
  233.         set(obj, MUIA_Window_Title, MyGetWindowTitle("CATALOGS", cwd->cwd_Title, sizeof(cwd->cwd_Title)));
  234.         set(obj, MUIA_Window_ActiveObject, catlist);
  235.  
  236.         DoMethod(parent,          MUIM_Window_AddChildWindow, obj);
  237.         DoMethod(obj,             MUIM_Notify, MUIA_Window_CloseRequest, TRUE,           MUIV_Notify_Application, 5, MUIM_Application_PushMethod, parent, 2, MUIM_Window_RemChildWindow, obj);
  238.         DoMethod(catlist,         MUIM_Notify, MUIA_NList_Active,        MUIV_EveryTime, obj,                     1, MUIM_CatalogsWin_ListChange);
  239.         DoMethod(updateButton,    MUIM_Notify, MUIA_Pressed,             FALSE,          obj,                     1, MUIM_CatalogsWin_Update);
  240.         DoMethod(printButton,     MUIM_Notify, MUIA_Pressed,             FALSE,          obj,                     1, MUIM_CatalogsWin_Print);
  241.         DoMethod(exitButton,      MUIM_Notify, MUIA_Pressed,             FALSE,          obj,                     3, MUIM_Set, MUIA_Window_CloseRequest, TRUE);
  242.         DoMethod(catlist,         MUIM_NList_Sort3, MUIV_NList_Sort3_SortType_1, MUIV_NList_SortTypeAdd_None, MUIV_NList_Sort3_SortType_Both);
  243.     }
  244.  
  245.     return (ULONG)obj;
  246. }
  247.  
  248. static ULONG __saveds mDispose( struct IClass *cl,
  249.                                 Object *obj,
  250.                                 struct opSet *msg )
  251. {
  252.     struct CatalogsWinData *cwd = INST_DATA(cl, obj);
  253.  
  254.     set(obj, MUIA_Window_Open, FALSE);
  255.     DoMethod(cwd->cwd_CatalogList, MUIM_NList_Clear);
  256.  
  257.     return (DoSuperMethodA(cl, obj, msg));
  258. }
  259.  
  260. static ULONG __saveds mUpdate( struct IClass *cl,
  261.                                Object *obj,
  262.                                Msg msg )
  263. {
  264.     struct CatalogsWinData *cwd = INST_DATA(cl, obj);
  265.     struct CatalogsCallbackUserData ud;
  266.  
  267.     ApplicationSleep(TRUE);
  268.     set(cwd->cwd_CatalogList, MUIA_NList_Quiet, TRUE);
  269.     DoMethod(cwd->cwd_CatalogList, MUIM_NList_Clear);
  270.  
  271.     ud.ud_List = cwd->cwd_CatalogList;
  272.     ud.ud_Count = 0;
  273.  
  274.     if (clientstate) {
  275.         ReceiveList(UpdateCallback, &ud);
  276.     } else {
  277.         IterateList(UpdateCallback, &ud);
  278.     }
  279.  
  280.     SetCountText(cwd->cwd_CatalogCount, ud.ud_Count);
  281.     MySetContents(cwd->cwd_CatalogText, "");
  282.  
  283.     set(cwd->cwd_CatalogList, MUIA_NList_Quiet, FALSE);
  284.     ApplicationSleep(FALSE);
  285.  
  286.     return 0;
  287. }
  288.  
  289. static ULONG __saveds mPrint( struct IClass *cl,
  290.                               Object *obj,
  291.                               Msg msg )
  292. {
  293.     PrintCatalogs(NULL);
  294.  
  295.     return 0;
  296. }
  297.  
  298. static ULONG __saveds mListChange( struct IClass *cl,
  299.                                    Object *obj,
  300.                                    Msg msg )
  301. {
  302.     struct CatalogsWinData *cwd = INST_DATA(cl, obj);
  303.     struct CatalogEntry *ce;
  304.  
  305.     if (ce = (struct CatalogEntry *)GetActiveEntry(cwd->cwd_CatalogList)) {
  306.         MySetContents(cwd->cwd_CatalogText, "%s \"%s\"", ce->ce_Address, ce->ce_Name);
  307.     }
  308.  
  309.     return 0;
  310. }
  311.  
  312. ULONG __asm __saveds CatalogsWinDispatcher( register __a0 struct IClass *cl,
  313.                                             register __a2 Object *obj,
  314.                                             register __a1 Msg msg )
  315. {
  316.     switch (msg->MethodID) {
  317.         case OM_NEW:                      return (mNew(cl, obj, (APTR)msg));
  318.         case OM_DISPOSE:                  return (mDispose(cl, obj, (APTR)msg));
  319.         case MUIM_CatalogsWin_Update:     return (mUpdate(cl, obj, (APTR)msg));
  320.         case MUIM_CatalogsWin_Print:      return (mPrint(cl, obj, (APTR)msg));
  321.         case MUIM_CatalogsWin_ListChange: return (mListChange(cl, obj, (APTR)msg));
  322.     }
  323.  
  324.     return (DoSuperMethodA(cl, obj, msg));
  325. }
  326.  
  327. void PrintCatalogs( char *filename )
  328. {
  329.     BPTR handle;
  330.  
  331.     if (handle = HandlePrintStart(filename)) {
  332.         PrintFOneLine(handle, "\n  Address  Version Language   Name\n\n");
  333.         IterateList(PrintCallback, (void *)handle);
  334.     }
  335.  
  336.     HandlePrintStop();
  337. }
  338.  
  339. void SendCatalogList( void )
  340. {
  341.     IterateList(SendCallback, NULL);
  342. }
  343.  
  344.